//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "treedir_zetadisk.h"
#include "Settings.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
using namespace std;

TFormTreeDirZeta *FormTreeDirZeta;
//---------------------------------------------------------------------------
__fastcall TFormTreeDirZeta::TFormTreeDirZeta(TComponent* Owner)
    : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TFormTreeDirZeta::TreeViewDirDblClick(TObject *Sender)
{
  current_item_node = (ItemNode*)TreeViewDir->Selected->Data;
  this->Close();
}
//---------------------------------------------------------------------------

void __fastcall TFormTreeDirZeta::FormActivate(TObject *Sender)
{
QueryTree->Close();
QueryTree->SQLConnection=FormSettings->SQLConnectionOracle;
QueryTree->SQL->Text= "select id, parent_id, lvl, l,r,cname,include_d,include_f,"
    "util.get_full_name_dir(id,'') as full_name_dir"
    " from xfiles  where what_is = 0"
    " and exists ( select 1 from xfiles n where xfiles.l<=n.l and xfiles.r>=n.r )"
    " order  by l";
QueryTree->Open();
TTreeNode *node,*top_node;
ItemNode  *item_node,*top_item_node;


item_node = new ItemNode;
item_node->id =QueryTree->FieldByName("id")->AsInteger;
item_node->lvl =QueryTree->FieldByName("lvl")->AsInteger;
item_node->r =QueryTree->FieldByName("r")->AsInteger;
item_node->l =QueryTree->FieldByName("l")->AsInteger;


node = TreeViewDir->Items->AddChildObject(NULL,
QueryTree->FieldByName("cname")->AsString,item_node);

QueryTree->Next();
stack <TTreeNode * > st;
st.push(node);

while (!QueryTree->Eof)
  {
    top_node = st.top();
    top_item_node = (ItemNode*)top_node->Data;
    st.pop();

    item_node = new ItemNode;
    item_node->id =QueryTree->FieldByName("id")->AsInteger;
    item_node->lvl =QueryTree->FieldByName("lvl")->AsInteger;
    item_node->r =QueryTree->FieldByName("r")->AsInteger;
    item_node->l =QueryTree->FieldByName("l")->AsInteger;
    item_node->cname =QueryTree->FieldByName("full_name_dir")->AsString;

  if(top_item_node->lvl>=item_node->lvl)
  {
   do
   {
    top_node = st.top();
    st.pop();
    top_item_node = (ItemNode*)top_node->Data;
   } while ( top_item_node->lvl>=item_node->lvl );
  }
   node = TreeViewDir->Items->AddChildObject(top_node,
   QueryTree->FieldByName("cname")->AsString,item_node);
   st.push(top_node);
   st.push(node);

  QueryTree->Next();
  }

}
//---------------------------------------------------------------------------

